package vip.web3.common.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建者: 豆沙包
 * 创建时间:2020/4/22 16:30
 * 版本: 1.0.0
 */


public class EnumsTypeUtils {

    /**
     * 根据枚举的字符串获取枚举的值
     * @param className 包名+类名
     * @return
     * @throws Exception
     */
    public static List<Map<String, Object>> getAllEnumByClassName(String className) throws Exception {
        // 1.得到枚举类对象
        Class<Enum> clz = (Class<Enum>) Class.forName(className);
        // 2.得到所有枚举常量
        Object[] objects = clz.getEnumConstants();
        String method = "getCode";
        if(className.equals("network.hotpot.common.enums.LevelPolicyEnum") || className.equals("network.hotpot.common.enums.RewardsTypeEnum")){
            method = "getType";
        }
        Method getCode = clz.getMethod(method);
        Method getDesc = clz.getMethod("getDesc");
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        for (Object obj : objects) {
            map = new HashMap<String, Object>();
//            String invoke = getCode.invoke(obj).toString();
            map.put("code", getCode.invoke(obj));
            map.put("desc", getDesc.invoke(obj));
            list.add(map);
        }
        return list;
    }


}
